//引入
const express = require('express');
const db = require('../../utils/db')

//配置路由器
const baogao = express.Router();

//接口编写

//获取
baogao.get('/baogao/query', (req, res) => {
  const page = req.query.page;
  const count = req.query.count;
  const sql = `select * from g_baogao limit ${(page - 1) * count},${count}`;
  const sql2 = `select count(*) as total from g_baogao;`;
  db.query(sql, (err, data) => {
    if (err) {
      console.log(err)
      res.send({ error: 1, msg: err })
    } else {
      db.query(sql2, (err2, data2) => {
        if (err2) {
          console.log(err2)
          res.send({ error: 1, msg: err2 })
        } else {
          console.log(data2)
          res.send({ error: 0, msg: '查询成功', data: data, total: data2[0].total })
        }
      })
    }
  })
})
//查询
baogao.post('/baogao/sousuo', (req, res) => {
  const sql = `SELECT * FROM g_baogao WHERE g_chaosong='${req.body.g_chaosong}' OR g_leixing='${req.body.g_leixing}' OR g_name='${req.body.g_name}'  OR g_baogao LIKE'%${req.body.g_baogao}%' ;`
  db.query(sql, (err, data) => {
      if (err) {
          console.log(err);
          res.send({ error: 1, msg: err })
      } else {
          res.send({ error: 0, msg: '查询成功', data: data })
      }
  })
})
//添加
baogao.post('/baogao/add',(req,res)=>{
  const sql=`insert into g_baogao values(null,'${req.body.g_date}','${req.body.g_baogao}','${req.body.g_name}','${req.body.g_leixing}'
  ,'${req.body.g_chaosong}')`;

  db.query(sql,(err,data)=>{
      if(err){
          console.log(err)
          res.send({error:1,msg:err})
      }else{
          res.send({error:0,msg:'添加成功',data:data})
      }
  })
})

// 删除
baogao.post('/baogao/del', (req, res) => {
  const sql = 'delete from g_baogao where g_id="' + req.body.id + '";';
  db.query(sql, (err, data) => {
      if (err) {
          console.log(err);
          res.send({ error: 1, msg: err })
      } else {
          res.send({ error: 0, msg: '删除成功' })
      }
  })
})

//导出
module.exports = baogao;